package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.PrintStream;

public class CheckOuter implements LibraryAction {

    private String ACTION_NAME = "2 - Checkout A Book";
    private PrintStream printStream;
    Library library;

    public CheckOuter(PrintStream printStream, Library library){
        this.printStream = printStream;
        this.library = library;
    }

    @Override
    public void performAction() {
        promptUserForBookSelection();

        String bookTitle = ConsoleReader.readLine();
        promptUserForUserName();
        String userName = ConsoleReader.readLine();
        checkOut(bookTitle,userName);
    }

    private void promptUserForUserName() {
        printStream.println("Please input your username");
    }

    @Override
    public String toString() {
        return ACTION_NAME;
    }

    private void promptUserForBookSelection() {
        printStream.println("What book would you like to check out?");
    }

    public void checkOut(String bookTitle, String ownerName) {
        for(LibraryBook book : library){
            if (bookTitle.equals(book.toString()) && book.isAvailable()){
                book.makeUnavailable();
                book.setOwner(ownerName);
                printStream.println("Thank you! Enjoy the book");
                return;
            }
        }
        printStream.println("That book is not available");
    }
}
